package com.bobi.tfca.utils;

import com.bobi.tfca.model.entity.MessageEntity;
import com.bobi.tfca.model.vo.MessageVO;
import org.joda.time.DateTime;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author 李文发
 * @date 2024/4/10
 */
public class DateTimeUtil {

    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static boolean isToday(Timestamp timestamp) {
        // 获取当前日期的Timestamp对象
        Timestamp currentTimestamp = Timestamp.valueOf(LocalDateTime.now());

        // 将Timestamp对象转换为LocalDate对象，以比较年、月和日
        LocalDate currentDate = currentTimestamp.toLocalDateTime().toLocalDate();
        LocalDate targetDate = timestamp.toLocalDateTime().toLocalDate();

        // 判断是否是今天
        return currentDate.equals(targetDate);
    }

    public static boolean isThisYear(Timestamp timestamp) {
        // 获取当前年份
        int currentYear = LocalDate.now().getYear();

        // 获取目标日期的年份
        int targetYear = timestamp.toLocalDateTime().toLocalDate().getYear();

        // 判断是否是今年
        return currentYear == targetYear;
    }

    public static Timestamp stringConvertTimestamp(String string) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
        LocalDateTime parse = LocalDateTime.parse(string, formatter);
        Timestamp timestamp = Timestamp.valueOf(parse);
        return timestamp;
    }

    public static String timestampConvertString(Timestamp timestamp) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
        String format = timestamp.toLocalDateTime().format(formatter);
        return format;
    }

    public static String localDateTimeConvertString(LocalDateTime localDateTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
        String format = localDateTime.format(formatter);
        return format;
    }

    public static LocalDateTime stringConvertLocalDateTime(String dateTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
        LocalDateTime time = LocalDateTime.parse(dateTime, formatter);
        return time;
    }
}
